var util = require("../util"), Reporter = require("../reporter"), webSender = require("../common/sender"), webPost = require("../common/post"), win = util.win, doc = win.document, validFn = /^(error|api|speed|sum|avg|percent|custom|msg|setPage|setConfig|behavior|performance)$/, Browser = function(e) {
    var r = this;
    return Reporter.call(r, e), r._initialPage = e.page && util.safetyCall(e.page, [], e.page + "") || null, 
    r._isRobot = util.isRobot(), r._health = {
        errcount: 0,
        apisucc: 0,
        apifail: 0
    }, r.beforeSend = function(e, t) {
        "error" === e ? r._health.errcount++ : "api" === e && r._health[t.success ? "apisucc" : "apifail"]++;
    }, !1 !== e.enableInstanceAutoSend && (r.initHandler(), r.initHook(), r.initFmpObserver(1e4), 
    r._conf && r._conf.behavior && "function" == typeof r.initBehavior && r.initBehavior()), 
    Object.defineProperty && win.addEventListener && Object.defineProperty(r, "pipe", {
        set: r.sendPipe
    }), r;
};

Browser.prototype = util.createObject(Reporter.prototype), util.ext(Reporter._root.dftCon, {
    uid: null,
    setUsername: null,
    ignoreUrlPath: null,
    ignoreApiPath: null,
    urlHelper: [ {
        rule: /\/([a-z\-_]+)?\d{2,20}/g,
        target: "/$1**"
    }, /\/$/ ],
    apiHelper: {
        rule: /\/([a-z\-_]+)?\d{2,20}/g,
        target: "/$1**"
    },
    ignoreUrlCase: !0,
    imgUrl: "https://arms-retcode.aliyuncs.com/r.png?",
    disableHook: !1,
    autoSendPv: !0,
    autoSendPerf: !0,
    enableSPA: !1,
    enableLinkTrace: !1,
    linkType: "arms",
    enableApiCors: !1,
    sendResource: !0,
    behavior: !0,
    enableConsole: !1,
    parseHash: function(e) {
        return (e ? util.cutUrlSearch(e.replace(/^#\/?/, "")) : "") || "[index]";
    },
    parseResponse: function(e) {
        if (!e || "object" != typeof e) return {};
        var r = e.code, t = e.msg || e.message || e.subMsg || e.errorMsg || e.ret || e.errorResponse || "";
        return "object" == typeof t && (r = r || t.code, t = t.msg || t.message || t.info || t.ret || JSON.stringify(t)), 
        {
            msg: t,
            code: r,
            success: !0
        };
    }
}), util.ext(Browser.prototype, {
    constructor: Browser,
    _super: Reporter,
    onReady: function(e) {
        var r = this;
        if (r.hasReady) return e();
        "complete" === doc.readyState ? (r.hasReady = !0, e()) : util.on(win, "load", function() {
            r.hasReady = !0, e();
        }, !0);
    },
    getPage: function(e) {
        var r = this._conf, t = r.page, i = location, o = i.host + i.pathname;
        return t && !e ? util.safetyCall(t, [], t + "") : this._initialPage || util.filterByRule(r.ignoreUrlCase ? o.toLowerCase() : o, r.ignoreUrlPath ? r.ignoreUrlPath : r.urlHelper);
    },
    setPage: function(e, r) {
        var t = this, i = t.prevPage;
        if (!1 !== r) {
            if (!e || e === i) return t;
            t.prevPage = e, clearTimeout(t.sendPVTimmer), t.handleUnload(1), t.resetPageview(), 
            t.sendPVTimmer = setTimeout(function() {
                t.sendPV();
            }, 10);
        } else t.prevPage = e;
        return t._conf.page = e, t;
    },
    setConfig: function(e, r) {
        if (e && "object" == typeof e) {
            util.verifyConfig(e), e = this.setImgUrl(e);
            var t = this._conf;
            if (this._conf = util.ext({}, t, e), !r) {
                var i = "disableHook";
                i in e && t[i] !== e[i] && (e[i] ? this.removeHook() : this.addHook()), (i = "enableSPA") in e && t[i] !== e[i] && this.bindHashChange(e[i]);
            }
        }
    },
    sendRequest: function(e) {
        webSender(e, this.getConfig("imgUrl"));
    },
    postData: function(e, r) {
        var t = {};
        t[r] = e[r], delete e[r];
        var i = "";
        "object" == typeof e && (i = util.serialize(e)), webPost(t, this.getConfig("imgUrl") + i + "&post_res=");
    },
    sendPipe: function(e) {
        var r = this;
        if (!e || !e.length) return r;
        try {
            if ("Array" === util.T(e[0])) return util.each(e, function(e) {
                return r.sendPipe(e);
            });
            if ("Array" !== util.T(e)) return r;
            var t = e.shift();
            if (!validFn.test(t)) return r;
            r[t].apply(r, e);
        } catch (i) {
            return util.warn("[retcode] error in sendPipe", i), r;
        }
    },
    sendHealth: function() {
        var e = util.ext({}, this._health);
        e.healthy = e.errcount > 0 ? 0 : 1, e.begin = Date.now();
        var r = e.begin - this.sBegin;
        e.stay = r, this._lg("health", e, 1), this._health = {
            errcount: 0,
            apisucc: 0,
            apifail: 0
        };
    },
    createInstance: function(e) {
        e = util.ext({
            pid: this._conf.pid
        }, e);
        var r = this.__proto__.constructor(e);
        return e.page && r.sendPV(), r;
    }
}), require("./behavior")(Browser, win), require("./handler")(Browser, win, doc), 
require("./fmp")(Browser, win, doc), require("./hook")(Browser, win), require("./hack")(Browser, win), 
Browser._super = Reporter, Browser._root = Reporter._root, Reporter.Browser = Browser, 
module.exports = Browser;